﻿//-------------------------------------------------------------------------------------------
// <copyright file="Accounts.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.Data
{
    using Converters;
    using Providers;
    using System.Collections.Generic;

    /// <summary>
    /// Dataset class for accessing Account entities.
    /// </summary>
    public class Accounts : DataSet
    {
        private Dictionary<string, Account> cache = new Dictionary<string, Account>();

        /// <summary>
        /// Initializes a new instance of the Accounts class.
        /// </summary>
        internal Accounts(IRippleContext context)
            : base(context)
        {
        }

        public Account Get(string id)
        {
            if (this.cache.ContainsKey(id))
            {
                return this.cache[id];
            }

            Account account = null;

            AccountInfoResult result = this.Context.Provider.AccountInfo(id, this.Context.LedgerIndex);
            if (result.Success)
            {
                account = result.account_data.ToEntity(this.Context);
                this.cache.Add(account.Id, account);
            }

            return account;
        }
    }
}